package org.ldh.web.servlet;

import org.ldh.domain.BookBorrow;
import org.ldh.service.BookBorrowService;
import org.ldh.service.impl.BookBorrowServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/bookBorrowServlet.action")
public class BookBorrowServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-------BookBorrowServlet--------");
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取请求参数
        String action = request.getParameter("action");
        System.out.println("action: "+action);
        //调用service层
        BookBorrowService bookBorrowService = new BookBorrowServiceImpl();

        //判断请求参数
        if(action != null && action.equals("list")){
            //查询列表
            //调用service层的查询方法
            List<BookBorrow> bookBorrows = bookBorrowService.findAll();
            //存储到request域
            request.setAttribute("bookBorrows",bookBorrows);
            //跳转到图书借阅管理列表页
            request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("add")){
            //添加
            //跳转到添加页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowAdd.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("save")){
            //保存
            //获取请求参数
            String bookId = request.getParameter("bookId");
            String bookName = request.getParameter("bookName");
            String borrowTime = request.getParameter("borrowTime");
            String returnTime = request.getParameter("returnTime");
            //通过id查询数据库
            BookBorrow bookBorrow = bookBorrowService.findById(bookId);
            //判断数据库中是否已经存在该信息
            if(bookBorrow == null){
                //数据库中不存在该信息
                //封装实体类
                BookBorrow bookBorrow2 = new BookBorrow();
                bookBorrow2.setBookId(bookId);
                bookBorrow2.setBookName(bookName);
                bookBorrow2.setBorrowTime(borrowTime);
                bookBorrow2.setReturnTime(returnTime);
                //保存到数据库
                bookBorrowService.save(bookBorrow2);
                //保存成功后，重新查询数据库
                List<BookBorrow> bookBorrows = bookBorrowService.findAll();
                //重新存储到request域
                request.setAttribute("bookBorrows",bookBorrows);
                //跳转到图书借阅管理列表页
                request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowList.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }else{
                //数据库中已经存在该信息
                //存储错误信息到request域
                request.setAttribute("error","该借阅信息已经存在");
                //跳转到图书借阅添加页面
                request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowAdd.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }
        }else if(action != null && action.equals("update")){
            //修改
            //获取请求参数
            String bookId = request.getParameter("bookId");
            //根据id查询
            BookBorrow bookBorrow = bookBorrowService.findById(bookId);
            //获取一些信息，保存到request域，在修改页面显示
            String bookName = bookBorrow.getBookName();
            String borrowTime = bookBorrow.getBorrowTime();
            String returnTime = bookBorrow.getReturnTime();
            request.setAttribute("bookId",bookId);
            request.setAttribute("bookName",bookName);
            request.setAttribute("borrowTime",borrowTime);
            request.setAttribute("returnTime",returnTime);
            //跳转修改页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowUpdate.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("reUpdate")){
            //更新
            //获取请求参数
            String bookId = request.getParameter("bookId");
            String bookName = request.getParameter("bookName");
            String borrowTime = request.getParameter("borrowTime");
            String returnTime = request.getParameter("returnTime");
            //封装到实体类
            BookBorrow bookBorrow = new BookBorrow();
            bookBorrow.setBookId(bookId);
            bookBorrow.setBookName(bookName);
            bookBorrow.setBorrowTime(borrowTime);
            bookBorrow.setReturnTime(returnTime);
            System.out.println(bookBorrow);
            //调用service层的更新方法
            bookBorrowService.update(bookBorrow);
            //修改完成之后，重新查询数据库
            List<BookBorrow> bookBorrows = bookBorrowService.findAll();
            //重新存储到request域
            request.setAttribute("bookBorrows",bookBorrows);
            //跳转到图书借阅管理列表页
            request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("delete")){
            //删除
            //获取请求参数
            String bookId = request.getParameter("bookId");
            //调用service层的删除方法
            bookBorrowService.delete(bookId);
            //删除完成之后,重新查询数据库
            List<BookBorrow> bookBorrows = bookBorrowService.findAll();
            //重新存储到request域
            request.setAttribute("bookBorrows",bookBorrows);
            //跳转到图书借阅管理列表页
            request.setAttribute("mainRight","/WEB-INF/pages/bookBorrowList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
